app
.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){
	$scope.changeValueMap = function(field) {
		if (field == 'bidInvoiceOffset') {
			
			var taxPercent = parseFloat($scope.valueMap['taxPercent']); // 统一税点

			$http.post('/api/inter/tcgInvoiceOffset/listItemByBid', {}, {params: {bid: $scope.valueMap['bidInvoiceOffset']}})
			.success(function (response) {
				if(response.code == 0) {
					$scope.subGrid.sonlist = [];
					var rows = response.data;
					if (taxPercent) {
						for (var i = 0; i < rows.length; i++) {
							rows[i]['offsetAmountNoTax'] = Math.round(rows[i]['offsetAmount'] / (1 + taxPercent) * 100) / 100; // 子件总额(不含税)
							rows[i]['subPurchasePriceNoTax'] = Math.round(rows[i]['offsetAmountNoTax'] / rows[i]['offsetNum'] * 100) / 100; // 子件单价(不含税)
							rows[i]['subTax'] =  Math.round((rows[i]['offsetAmount'] - rows[i]['offsetAmountNoTax']) * 100) / 100; // 子件总税额
						}
					}
					$scope.subGrid.sonlist = rows;
				} else {
					alert(response.msg);
				}
			});
		} else if (field == 'taxPercent') {
			var taxPercent = parseFloat($scope.valueMap['taxPercent']); // 统一税点
			for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
				$scope.subGrid.sonlist[i]['offsetAmountNoTax'] = Math.round($scope.subGrid.sonlist[i]['offsetAmount'] / (1 + taxPercent) * 100) / 100; // 子件总额(不含税)
				$scope.subGrid.sonlist[i]['subPurchasePriceNoTax'] = Math.round($scope.subGrid.sonlist[i]['offsetAmountNoTax'] / $scope.subGrid.sonlist[i]['offsetNum'] * 100) / 100; // 子件单价(不含税)
				$scope.subGrid.sonlist[i]['subTax'] =  Math.round(($scope.subGrid.sonlist[i]['offsetAmount'] - $scope.subGrid.sonlist[i]['offsetAmountNoTax']) * 100) / 100; // 子件总税额
			}
		}
	}
}